//统一网络请求封装
import config from "../../config.js"


const baseUrl = config.getBasePath();
export const HttpUtil = (parmer) => {
	return new Promise(function(resolve, reject) {

		uni.showLoading({
			title: "获取数据中"
		})
		uni.request({
			url: baseUrl + parmer.url,
			method: parmer.method,
			data: parmer.data,
			header: {
				'content-type': 'application/x-www-form-urlencoded'
			},
			success: (res) => {
				if (res.statusCode == 200) {
					resolve(res);
					console.log("获取数据成功", res);
				} else {
					uni.showToast({
						title: "操作失败"
					})
				}

			},
			fail: (error) => {
				uni.showToast({
					title: "获取失败"
				})
				reject(error);
				console.log("获取数据失败", error);

			},
			complete() {
				uni.hideLoading();
				console.log("请求完成");
			}
		})
	})

}


export const postData = (parmer) => {
	parmer.method = 'POST';
	console.log("baseUrl", baseUrl);
	console.log("url", parmer.url);
	console.log(parmer.method);
	console.log("请求的参数",parmer.data);
	return HttpUtil(parmer);
}

export const getData = (parmer) => {
	parmer.method = 'GET';
	console.log("baseUrl", baseUrl);
	console.log("url", parmer.url);
	console.log(parmer.method);
	console.log(parmer.data);
	return HttpUtil(parmer);
}
